package com.xiyu.service.repository.elder.register;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.elder.register.ElderVisitFetcher;
import com.xiyu.service.vo.elder.visit.VisitPageQueryInput;
import com.xiyu.service.model.elder.register.ElderVisit;
import com.xiyu.service.model.elder.register.ElderVisitTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface ElderVisitRepository extends JRepository<ElderVisit, Long> {
    ElderVisitTable elderVisitTable = ElderVisitTable.$;

    default Page<ElderVisit> pageQuery(VisitPageQueryInput inputVO){
        return sql().createQuery(elderVisitTable)
                .whereIf(StringUtils.hasText(inputVO.getVisitMobile()), () -> elderVisitTable.visitMobile().like(inputVO.getVisitMobile()))
                .whereIf(inputVO.getStatus() != null, elderVisitTable.status().eq(inputVO.getStatus()))
                .whereIf(StringUtils.hasText(inputVO.getVisitName()), () -> elderVisitTable.visitName().like(inputVO.getVisitName()))
                .whereIf(inputVO.getIsReserve() != null, elderVisitTable.isReserve().eq(inputVO.getIsReserve()))
                .orderBy(elderVisitTable.id().desc())
                .select(elderVisitTable.fetch(ElderVisitFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<ElderVisit> findDetailById(Long id){
        return sql().createQuery(elderVisitTable)
                .where(elderVisitTable.id().eq(id))
                .select(elderVisitTable.fetch(ElderVisitFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchOptional();
    }

}